using System.IO;
using System.Windows.Input;
using Bookvar.ObjectModel;
using Bookvar.UI.Commands;

namespace Bookvar.UI.ViewModel
{
    internal partial class MindmapViewModel
    {
        private abstract class AddResourceCommandBase : CommandModel
        {
            private readonly MindmapViewModel viewModel;

            protected abstract ResourceType ResourceType { get; }

            protected AddResourceCommandBase( ICommand command, MindmapViewModel viewModel )
                : base( command )
            {
                this.viewModel = viewModel;
            }

            protected internal override void OnExecute( object sender, ExecutedRoutedEventArgs e )
            {
                string fileName = e.Parameter as string;

                if ( File.Exists( fileName ) )
                {
                    var resource = new Resource( Path.GetFileName( fileName ), ResourceType );

                    var action = TopicActions.CreateAddResourceAction(
                        resource, fileName, viewModel.SelectedTopic, viewModel.Mindmap );

                    ActionService.Execute( action );
                }
            }

            protected internal override void OnCanExecute( object sender, CanExecuteRoutedEventArgs e )
            {
                e.CanExecute = viewModel.SelectedTopic != null;
                e.Handled = true;
            }
        }
    }
}